from aip import AipImageClassify

import os
import cv2
#定义中文显示函数
from PIL import Image,ImageFont,ImageDraw
import numpy as np
# 中文显示函数
def draw_chinese(img, text, org):
    img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))#色彩通道转换为RGB
    font = ImageFont.truetype(font='msyh.ttc', size=40)
    draw = ImageDraw.Draw(img_pil)
    draw.text(org, text, font=font, fill=(255, 0, 255))  # 红色RGB=(255,0,0)
    img_cv = np.array(img_pil)                         
    img = cv2.cvtColor(img_cv, cv2.COLOR_RGB2BGR) #色彩通道转换回RGB    
    return img
if __name__ == '__main__':
    """ 你的 APPID AK SK """
    APP_ID = '120213318'
    API_KEY = 'YwMeo9Qiuhf3ajJUJwNcJ6Oa'
    SECRET_KEY = 'AgYRrVi2SgwarlU9xg72T4fUiztv063X'
    client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

    # 大问题？怎么快速简介的使用菜品识别函数？
        # 1.读入一个图片
        # 2.调用菜品识别函数
    img_path = './aip/rjm.jpg' 
    with open(img_path, 'rb') as fp:
        image = fp.read()
    # 存起
    result = client.dishDetect(image,{"top_num": 3,"filter_threshold": 0.1,"baike_num":5})
    print(result)
    #导入必要的模块

    #在工作目录下读取一张图片
    img = cv2.imread(img_path, cv2.IMREAD_COLOR) # 按原图模式读取程变量
    #先显示查看图像
    # cv2.imshow('IMG', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # 统一左上角顶点坐标
    pt1 = (30,30)#左上角顶点坐标
    pt2 = (100,100)#右下角顶点坐标
    # 向Text输入第一个结果
    text1 = result['result'][0]['name']
    text2 = result['result'][0]['probability']
    text = text1 + '\n' + text2
    img  = draw_chinese(img, text,pt1)
    #图像显示与等待关闭
    cv2.imshow('IMG', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()